package com.zjw.controller;


import com.zjw.annotation.domain.ResponseResult;
import com.zjw.annotation.domain.entity.Menu;
import com.zjw.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/system/menu")
@PreAuthorize("@Ps.hasPermission('system:menu:list')")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @GetMapping("/list")
    @PreAuthorize("@Ps.hasPermission('system:menu:query')")
    public ResponseResult getMenuList(String status, String menuName) {
        return menuService.getMenuList(status, menuName);
    }

    @PostMapping
    @PreAuthorize("@Ps.hasPermission('system:menu:add')")
    public ResponseResult addMenu(@RequestBody Menu menu) {
        return menuService.addMenu(menu);
    }

    @GetMapping("/{id}")
    @PreAuthorize("@Ps.hasPermission('system:menu:query')")
    public ResponseResult getMenu(@PathVariable Long id) {

        return menuService.getMenuById(id);
    }

    @PutMapping
    @PreAuthorize("@Ps.hasPermission('system:menu:edit')")
    public ResponseResult editMenu(@RequestBody Menu menu) {
        return menuService.editMenu(menu);
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("@Ps.hasPermission('system:menu:remove')")
    public ResponseResult removeMenu(@PathVariable Long id) {
        System.out.println(id);
        return menuService.removeMenu(id);
    }


    /**
     * 获取所有菜单树 后台管理
     *
     * @return {@link ResponseResult}
     */
    @GetMapping("/treeselect")
    @PreAuthorize("@Ps.hasPermission('system:menu:query')")
    public ResponseResult getMenuTree() {
        return menuService.getTreeMenuList();
    }


    /**
     * 按 ID 获取角色菜单树
     *
     * @param id 编号
     * @return {@link ResponseResult}
     */
    @GetMapping("/roleMenuTreeselect/{id}")
    @PreAuthorize("@Ps.hasPermission('system:menu:query')")
    public ResponseResult getRoleMenuTreeById(@PathVariable Long id){
        return menuService.getRoleMenuTreeById(id);
    }

}
